package Controllers;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;

import Bean.BeanOrder;
import Bean.BeanRating;
import Impl.OrderServiceImpl;
import Impl.RatingServiceImpl;
import exception.BooksNotFoundException;

@Controller
public class ShowRating extends HttpServlet {
	RatingServiceImpl ratingService;
	 @RequestMapping("/ShowRating")
		protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// TODO Auto-generated method stub
	    	response.setContentType("text/html;charset=UTF-8");  
	    	JSONArray arr = new JSONArray();
	    	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
	    	ratingService=(RatingServiceImpl)applicationContext.getBean("RatingServiceImpl");
			Collection<BeanRating> ratings;
			String userid=request.getParameter("userid");
			try {
				ratings = ratingService.getratings((String)request.getSession().getAttribute("userid"));	
				for(BeanRating r:ratings) {
					arr.add(r.toJson());
				}
			} catch (BooksNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.getWriter().append(arr.toJSONString());
		}
}
